﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Wise.TimeWarrior.Helpers
{
    /// <summary>
    /// Time Picker Editor used in configuration form
    /// </summary>
    public class TimePickerEditor : UITypeEditor
    {

        IWindowsFormsEditorService editorService;
        DateTimePicker picker = new DateTimePicker();
        string time;
        /// <summary>
        /// .ctor
        /// </summary>
        public TimePickerEditor()
        {

            picker.Format = DateTimePickerFormat.Custom;
            picker.CustomFormat = "HH:mm";
            picker.ShowUpDown = true;

        }
        /// <summary>
        /// <see cref="UITypeEditor.EditValue(System.IServiceProvider,object)"/>
        /// </summary>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            if (provider != null)
            {
                this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (this.editorService != null)
            {
                if (value == null)
                {
                    time = DateTime.Now.ToString("HH:mm");
                }

                this.editorService.DropDownControl(picker);

                value = picker.Value.ToString("HH:mm");
            }

            return value;

        }

        /// <summary>
        /// <see cref="UITypeEditor.GetEditStyle(ITypeDescriptorContext)"/>
        /// </summary>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

    }


}
